package com.group.eams.controller;






import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.permission.Permission;
import com.group.eams.pojo.dto.DepartQueryDto;
import com.group.eams.pojo.entity.Depart;
import com.group.eams.service.DepartService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门管理(Depart)表控制层
 *
 * @author 
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/depart")
public class DepartController  {
    /**
     * 服务对象
     */
    @Autowired
    private DepartService departService;

    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "部门列表",httpMethod = "GET")
    public Result list(@RequestBody DepartQueryDto departQueryDto){

        //1.准备分页条件
        Page<Depart> page=new Page<>(departQueryDto.getPage(),departQueryDto.getLimit());

        //2.设置查询条件
        LambdaQueryWrapper<Depart> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.like(!StrUtil.isEmpty(departQueryDto.getName()),
                        Depart::getName,departQueryDto.getName()).
                eq(!StrUtil.isEmpty(departQueryDto.getCode()),Depart::getCode,
                        departQueryDto.getCode()).
                eq(departQueryDto.getDepartType()!=null,Depart::getDepartType,departQueryDto.getDepartType());

        //3.查询分页数据
        Page<Depart> departPage = departService.page(page, lambdaQueryWrapper);

        //返回结果
        return Result.buildSuccess(departPage.getTotal(),departPage.getRecords());
    }

    @RequestMapping(value = "/find",method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找部门")
    public Result find(@RequestParam Long id){
        Depart depart = departService.getById(id);
        return Result.buildSuccess(depart);
    }
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ApiOperation(value = "查找所有部门")
    public Result findAll(){
        List<Depart> departList = departService.list();
        return Result.buildSuccess(departList);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation("添加部门")
    public Result add(@RequestBody Depart depart){
        boolean result = departService.save(depart);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ApiOperation("修改部门")
    public Result update(@RequestBody Depart depart){
        boolean result = departService.updateById(depart);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    @ApiOperation("根据id删除部门")
    public Result delete(@RequestParam Long id){
        boolean result = departService.removeById(id);
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }


    @RequestMapping(value = "/batchRemove",method = RequestMethod.POST)
    @ApiOperation("批量删除部门")
    public Result batchRemove(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = departService.removeByIds(batchDeleteDto.getIds());
        return result?Result.buildSuccess():Result.buildFail(ResultEnum.OPTION_FAIL);
    }
    
}

